package com.home.moon.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.home.moon.chat.mapper.ChatChannelMapper;
import com.home.moon.chat.mapper.ChatsMapper;
import com.home.moon.chat.model.ChatChannel;
import com.home.moon.chat.model.Chats;
import com.home.moon.chat.model.Users;
import com.home.moon.chat.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author auto
 */
@Service
public class ChatServiceImpl implements BaseService {
    @Autowired
    private ChatsMapper chatsMapper;
    @Autowired
    private ChatChannelMapper chatChannelMapper;

    private ReentrantLock lock = new ReentrantLock();

    public int addChat(Chats chat){
        if(StringUtils.isNotBlank(chat.getChannelNo())){
            ChatChannel chatChannel = new ChatChannel();
            chatChannel.setNo(UUID.randomUUID().toString());
            chatChannel.setUserId(chat.getUserId());
            chatChannel.setCreateTime(new Date());
            chatChannelMapper.insert(chatChannel);
            chat.setChannelNo(chatChannel.getNo());
        }
        return chatsMapper.insert(chat);
    }

    public List<Chats> getChats(Users user){
        LambdaQueryWrapper<Chats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Chats::getUserId,user.getId());
        return chatsMapper.selectList(wrapper);
    }
}
